<?php

namespace app\api\service;
use app\common\model\Parameter;
use app\common\model\User;
use app\common\model\UserAmountRewardDetailed;
use app\Response;

class InviteService
{
    /*** 邀请奖励页面
    */
    public static function inviteViewService($user_id){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if($userInfo['type'] != 23){
            return Response::wrapError(0,"账户非达人用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $info = [];
        $ua_where[] = ["uid","=",$user_id];
        $ua_where[] = ["state","=",1];
        $ua_where[] = ["type","in","1,2,3"];
        $userAmountCount = UserAmountRewardDetailed::orderRewardSum($ua_where,"amount_money");
        $info["invite_reward_money"] = $userAmountCount;
        $info["invite_wiseman_num"] = User::dataCount(["superor_id"=>$user_id,"enable"=>1,"type"=>23]);
        $info["invite_merchant_num"] = User::dataCount(["superor_id"=>$user_id,"enable"=>1,"type"=>22]);
        $info["expert_rebate"] = Parameter::parameterValue(["id"=>2]);
        $info["merchant_rebate"] = Parameter::parameterValue(["id"=>3]);
        return Response::wrapData($info);
    }

    /*** 邀请用户列表
     */
    public static function intviteUserListService($user_id,$page,$num,$type){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if($userInfo['type'] != 23){
            return Response::wrapError(0,"账户非达人用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $list = User::dataList(["superor_id"=>$user_id,"enable"=>1,"type"=>$type],$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $k => $v){
                $list[$k]["addtime"] = !empty($v["addtime"])?date("Y-m-d H:i:s"):"";
            }
            $count = User::dataCount(["superor_id"=>$user_id,"enable"=>1,"type"=>$type]);
        }else{
            $list = [];
            $count = 0;
        }
        return Response::wrapData($list,$count);
    }


    /*** 邀请奖励明细
     */
    public static function intviteRewardListService($user_id,$page,$num,$type){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if($userInfo['type'] != 23){
            return Response::wrapError(0,"账户非达人用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $where[] = ["ua.uid","=",$user_id];
        if(!empty($type)){
            $where[] = ["u.type","=",$type];
        }
        $list = UserAmountRewardDetailed::rewardDetailsList($page,$num,$where,"ua.*,u.nickname,u.phone");
        if(!empty($list)){
            $list = $list->toArray();
            $count = UserAmountRewardDetailed::rewardDetailsCount($where,"ua.*,u.nickname,u.phone");
            foreach ($list as $k => $v){
                if(empty($v["nickname"])){
                    $list[$k]["nickname"] = substr_replace($v['phone'],'****',3,4);
                }
                $list[$k]["addtime"] = !empty($v["addtime"])?date("Y-m-d H:i:s",$v["addtime"]):"";
            }
        }else{
            $count = 0;
            $list = [];
        }
        return Response::wrapData($list,$count);
    }
}